#include "Stdafx.h"
#include "gditobitmap.h"
namespace convertor{
		Bitmap ^GDItoCImgConv::CreateBitmapFromCImg(CImg<float> & img)
		{
			int			width = img.width();
			int			height = img.height();
			Bitmap		^bm = gcnew Bitmap(width, height, PixelFormat::Format32bppArgb);

			// Step through cimg and bitmap, copy values to bitmap.
			const int	nPlanes = 4; // NOTE we assume alpha plane is the 4th plane.
			System::Drawing::Rectangle	rc(0, 0, width, height);
			// LockBits on destination bm.
			//BitmapData^ dataDest;
			BitmapData^	dataDest = bm->LockBits(rc, ImageLockMode::ReadOnly | ImageLockMode::WriteOnly, PixelFormat::Format32bppArgb); //Last Argumnet dataDest Reduced
			//if (s==OK)
			{
				BYTE*  pStartDest = (BYTE *)dataDest->Scan0.ToPointer();

				UINT nLines = dataDest->Height;		// number of lines
				UINT nPixels = dataDest->Width;		// number of pixels per line
				UINT dPixelDest = nPlanes;			// pixel step in destination
				UINT dLineDest = dataDest->Stride;	// line step in destination

				BYTE * pLineDest = pStartDest;

				for (UINT y = 0; y < nLines; y++)	// loop through lines
					{
					BYTE *pPixelDest = pLineDest;

					for (UINT x = 0; x < nPixels; x++) // loop through pixels on line
						{
						/* T &  operator() (const unsigned int x, const unsigned int y=0, const unsigned int z=0, const unsigned int v=0) const
						Fast access to pixel value for reading or writing.
						*/
						float	redCompF = img(x,y,0,0);
						if (redCompF < 0.0f)
						redCompF = 0.0f;
						else if (redCompF > 255.0f)
						redCompF = 255.0f;
						float	greenCompF = img(x,y,0,1);
						if (greenCompF < 0.0f)
						greenCompF = 0.0f;
						else if (greenCompF > 255.0f)
						greenCompF = 255.0f;
						float	blueCompF = img(x,y,0,2);
						if (blueCompF < 0.0f)
						blueCompF = 0.0f;
						else if (blueCompF > 255.0f)
						blueCompF = 255.0f;

						BYTE	redComp = BYTE(redCompF + 0.4999f);
						BYTE	greenComp = BYTE(greenCompF + 0.4999f);
						BYTE	blueComp = BYTE(blueCompF + 0.4999f);
						*(pPixelDest) = blueComp;
						*(pPixelDest+1) = greenComp;
						*(pPixelDest+2) = redComp;
						*(pPixelDest+3) = 255; // alpha
						pPixelDest += dPixelDest;
						}
				pLineDest += dLineDest;
				}
			bm->UnlockBits(dataDest);
			}
		return bm;	
		//TODO:debug point
		//bm->Save("C:\\Users\\serak\\Pictures\\ser2.bmp",ImageFormat::Bmp);
		}
		void GDItoCImgConv::FillCImgFromBitmap(CImg<float> & img,Bitmap ^bm)
		{
			// 		int	width = img.width();
			// 		int	height = img.height();
			int width=bm->Width;
			int height=bm->Height;
			CImg<float> tempimg(width,height,1,3);
			img=tempimg;
			// Step through cimg and bitmap, copy values from bitmap to img.
			const int	nPlanes = 4; // NOTE we assume alpha plane is the 4th plane.
			System::Drawing::Rectangle	rc(0, 0, width, height);
			// LockBits on source
			//BitmapData dataSrc;
			BitmapData^ dataSrc = bm->LockBits(rc, ImageLockMode::ReadOnly, PixelFormat::Format32bppArgb);
			/*if (s == Ok)*/
			{
				BYTE * pStartSrc = (BYTE *) dataSrc->Scan0.ToPointer();

				UINT nLines = dataSrc->Height;		// number of lines
				UINT nPixels = dataSrc->Width;		// number of pixels per line
				UINT dPixelSrc = nPlanes;			// pixel step in source
				UINT dLineSrc = dataSrc->Stride;		// line step in source

				BYTE * pLineSrc = pStartSrc;

				for (UINT y = 0; y < nLines; y++)	// loop through lines
				{
					BYTE	*pPixelSrc = pLineSrc;

					for (UINT x = 0; x < nPixels; x++) // loop through pixels on line
					{
						BYTE	redComp = *(pPixelSrc+2);
						BYTE	greenComp = *(pPixelSrc+1);
						BYTE	blueComp = *(pPixelSrc+0);

						img(x,y,0,0) = float(redComp);
						img(x,y,0,1) = float(greenComp);
						img(x,y,0,2) = float(blueComp);
						pPixelSrc += dPixelSrc;
					}
					pLineSrc += dLineSrc;
				}
				bm->UnlockBits(dataSrc);
			}
			//TODO:debug point of cimg
			//img.save("C:\\Users\\serak\\Pictures\\ser.bmp");
		}
	}